<template>
  <Card title="查看运费">
    <AdvancedTable
      :pagination="pagination"
      :dataSource="tableInfo.list"
      :columns="tableInfo.columns"
      :loading="tableInfo.loading"
      @pageChange="handlePageChange"
      @pageSizeChange="handlePageSizeChange">
    </AdvancedTable>
    <p class="actions">
      <Button @click="handleBack()">返回上一层</Button>
    </p>
  </Card>
</template>
<script>
import { mapMutations } from 'vuex';
import AdvancedTable from '@/components/supplier/AdvancedTable'; // 表格组件
import { queryViewFreightListService } from '@/api/supplier/management/freight';
import { freight } from '../constants';
const { VIEW_FREIGHT_TABLE_COLUMNS } = freight

export default {
  components: {
    AdvancedTable,
  },
  data () {
    return {
      fromPageName: '',
      tableInfo: {
        list: [],
        selection: [],
        columns: VIEW_FREIGHT_TABLE_COLUMNS,
        loading: false
      },
      pagination: {
        total: 0,
        current: 1,
        size: 10,
        pages: 0
      },
    }
  },
  created () {
    this.getTableData();
  },
  methods: {
    ...mapMutations('supplier/baseList', [
      'setFormItemData',
    ]),
    getTableData (current = this.pagination.current, size = this.pagination.size) {
      this.tableInfo.loading = true
      queryViewFreightListService(Object.assign({ current, size }, this.formData)).then(({ result }) => {
        const { records: list, current, size, pages, total } = result
        this.tableInfo.list = list
        this.pagination = Object.assign(this.pagination, { current, size, pages, total })
        this.tableInfo.loading = false
      })
    },
    handlePageChange (current) {
      this.getTableData(current)
    },
    handlePageSizeChange (size) {
      this.getTableData(1, size)
    },
    handleBack () {
      this.$router.go(-1)
    }
  },
  beforeRouteEnter (to, from, next) {
    // 保存入口地址
    // console.log('========beforeRouteEnter=======')
    // console.log('to :>> ', to);
    // console.log('from :>> ', from);
    next((vm) => {
      vm.fromPageName = from.name
    })
  },
  beforeRouteLeave (to, from, next) {
    // 如果不是正常返回清空列表页面查询表单字段
    // console.log('========beforeRouteLeave=======')
    // console.log('to :>> ', to);
    // console.log('from :>> ', from);
    if (this.fromPageName !== to.name) {
      this.setFormItemData(null);
    }
    next()
  }
}
</script>
<style scoped>
.actions {
  margin-top: 20px;
  text-align: center;
}
</style>
